import { arrayToString } from "./do-string";

import { ElMessage, ElMessageBox, ElNotification } from 'element-plus';
/**
@description 批量删除
@param tableRef table对象
@param deleteRow 删除函数
*/
export const batchDelete = (tableRef: any, deleteRow: any) => {
  //获取选中的行
  let ids: string[] | null = tableRef.value
    .getSelectionRows()
    .map((r: { id: string }) => {
      return r.id;
    });
  if (ids == null || ids.length <= 0) {
    ElMessage.error("必须至少选中一条数据")
  } else {
    deleteRow(arrayToString(ids));
  }
};

/**
@description 删除完成后触发
@param err 错误消息
@param successCallBack 请求成功后执行的函数
@param failureCallBack 失败后执行的函数
*/
export const deleteDone = (
  err: any,
  successCallBack: any = null,
  failureCallBack: any = null,
) => {
  if (err) {
    if (failureCallBack) {
      failureCallBack();
    } else {
      ElMessage.error("删除失败!,错误信息：" + err);
    }
  } else {
    if (successCallBack) {
      successCallBack();
    } else {
      ElMessage.success("删除成功");
    }
  }
};

/**
 * 查询列表完成时调用
 * @param err 错误信息
 * @param res 返回信息
 * @param tableData 表格对象
 * @param successCallBack 成功回调
 * @param failureCallBack 失败回调
 */
export const queryTableDone = (
  err: any,
  res: any,
  tableData: any,
  successCallBack: any = null,
  failureCallBack: any = null,
) => {
  if (err) {
    if (failureCallBack) {
      failureCallBack();
    } else {
      ElMessage.error("" + err);
    }
  } else {
    if (successCallBack) {
      successCallBack();
    } else {
      const data = res?.data?.data;
      tableData.data = data?.items;
      tableData.total = data?.totalCount;
      tableData.loading = false;
    }
  }
};

/**
 * 查询完成时调用
 * @param err 错误信息
 * @param successCallBack 成功回调
 * @param failureCallBack 失败回调
 */
export const queryDone = (
  err: Error|null,
  successCallBack: any = null,
  failureCallBack: any = null,
) => {
  if (err) {
    if (failureCallBack) {
      failureCallBack();
    } else {
      ElMessage.error("" + err.message);
    }
  } else {
    if (successCallBack) {
      successCallBack();
    }
  }
};

/**
@description 请求完成后触发
@param err 错误消息
@param successCallBack 请求成功后执行的函数
@param failureCallBack 失败后执行的函数
*/
export const requestDone = (
  err: any,
  successCallBack: any = null,
  failureCallBack: any = null,
) => {
  if (err) {
    if (failureCallBack) {
      failureCallBack();
    } else {
      // ElMessage({
      //   type: "error",
      //   message: "" + err,
      //   dangerouslyUseHTMLString: true,
      // });

      ElMessage.error("" + err);
    }
  } else {
    if (successCallBack) {
      successCallBack();
    }
  }
};

/**
 * 下载文件
 * @param err
 * @param successCallBack
 * @param failureCallBack
 */
export const requestDownloadDone = (
  err: any,
  res: any,
  filename: string,
  successCallBack: any = null,
  failureCallBack: any = null,
) => {
  if (err) {
    if (failureCallBack) {
      failureCallBack();
    } else {
      // ElMessage({
      //   type: "warning",
      //   message: "文件下载异常",
      //   dangerouslyUseHTMLString: true,
      // });

      ElMessage.warning("文件下载异常" );
    }
  } else {
    if (res.data) {
      //执行下载
      const blob = new Blob([res.data as BlobPart]);
      //可以自定义
      filename = `${filename}`;
      const link = document.createElement("a"); //创建a标签
      link.style.display = "none";
      link.href = window.URL.createObjectURL(blob);
      link.download = filename; //导出文件名
      document.body.appendChild(link);
      link.click(); //执行下载
      URL.revokeObjectURL(link.href); //释放url
      document.body.removeChild(link); //释放标签
      if (successCallBack) {
        successCallBack();
      }
    } else {

      ElMessage.warning("文件下载异常" );
      // ElMessage({
      //   type: "warning",
      //   message: "文件下载异常",
      //   dangerouslyUseHTMLString: true,
      // });
    }
  }
};
